//
//  RightViewController.m
//  FoodList
//
//  Created by 王金晶 on 16/4/6.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "RightViewController.h"
#import "BaseViewController.h"
#import "TableViewCell.h"
#import "LoginViewController.h"
#import "RegiestViewController.h"
@interface RightViewController ()<UITableViewDataSource,UITableViewDelegate,LoginViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *head;
@property (weak, nonatomic) IBOutlet UIButton *login;
@property (weak, nonatomic) IBOutlet UIButton *regiest;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *background;
@property(nonatomic,strong)NSArray *titleArray;
@end

@implementation RightViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
     self.view.frame = CGRectMake(-280, 0, 280, Height);
    //添加阴影
    self.view.layer.shadowOpacity = 0.7;
    self.view.layer.shadowOffset = CGSizeMake(6, 0);
    self.view.layer.shadowColor = [UIColor grayColor].CGColor;
    
    //添加轻扫手势
    UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(change:)];
    swipeleft.direction = UISwipeGestureRecognizerDirectionLeft ;
    [self.view addGestureRecognizer:swipeleft];
    
    _titleArray = @[@"收藏",@"更换皮肤",@"设置",@"关于我们"];
    
    self.head.layer.cornerRadius = 25;
    self.head.layer.masksToBounds = YES;
    
    //登录按钮
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"loadState"])
    {
        [self.login setTitle:[[NSUserDefaults standardUserDefaults]objectForKey:@"name"] forState:UIControlStateNormal];
    }
    [self.login addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    //注册
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"loadState"])
    {
        [self.regiest setTitle:@"注销" forState:UIControlStateNormal];
    }

    [self.regiest addTarget:self action:@selector(regiest:) forControlEvents:UIControlEventTouchUpInside];
    
    //头像
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"loadState"]) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
        if (data != nil) {
            UIImage *image = [UIImage imageWithData:data];
            _head.image = image;
       }
    }

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 1000;
    
}
- (void)login:(UIButton *)button
{
    LoginViewController *loginVc = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
    loginVc.delegate1 = self;
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:loginVc];
    [self presentViewController:nav animated:YES completion:nil];
    
}
- (void)regiest:(UIButton *)button
{   if([[NSUserDefaults standardUserDefaults]boolForKey:@"loadState"])
{
    [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"loadState"];
    [self.login setTitle:@"登录" forState:UIControlStateNormal];
    [self.regiest setTitle:@"注册" forState:UIControlStateNormal];
}

    RegiestViewController *regietVc = [[RegiestViewController alloc]initWithNibName:@"RegiestViewController" bundle:nil];
    [self presentViewController:regietVc animated:YES completion:nil];

}
//协议方法
- (void)changeRightViewController
{
    self.view.frame = CGRectMake(-35, 0, 280, Height);
}
//协议方法
- (void)changetitle:(NSString *)string
{
    [self.login setTitle:[[NSUserDefaults standardUserDefaults]objectForKey:@"name"] forState:UIControlStateNormal];
    [self.regiest setTitle:@"注销" forState:UIControlStateNormal];
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
    if (data != nil) {
        UIImage *image = [UIImage imageWithData:data];
        _head.image = image;
    }
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    

}

- (void)change:(UISwipeGestureRecognizer *)swipe
{
    if (self.view.frame.origin.x == -280) {
        return;
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame = CGRectMake(-280 , 0, 280, Height);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            self.view.frame = CGRectMake(-270, 0, 280, Height);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                self.view.frame = CGRectMake(-280 , 0, 280, Height);
            }];
        }];
    }];
    //让代理执行协议的方法
    if (_delegate && [_delegate respondsToSelector:@selector(close)]) {
        [_delegate close];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  NSArray *imageArray = @[@"collect-1",@"set",@"eyes",@"us"];
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"login"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil].firstObject;
        cell.name.text = _titleArray[indexPath.row];
        cell.icon.image = [UIImage imageNamed:imageArray[indexPath.row]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
    }
    else if(indexPath.row == 1)
    {
        [self alertAction];
        
    }else if (indexPath.row == 2)
    {
        //清除缓存
        
    }
    else if(indexPath.row == 3)
    {
        
    }
}
#pragma mark ----------alectAcion--------
- (void)alertAction
{  UIAlertController *control = [UIAlertController alertControllerWithTitle:@"选取背景图片" message:@"更换皮肤" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            picker.delegate = self;
            //允许编辑
            picker.editing = YES;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }];
    [control addAction:action];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.delegate = self;
            picker.editing = YES;
            [self presentViewController:picker animated:YES completion:nil];
        }
    }];
    [control addAction:action1];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"还原系统皮肤" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.background.image = [UIImage imageNamed:@"back-1"];
    }];
    [control addAction:action3];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [control addAction:action2];

    [self presentViewController:control animated:YES completion:nil];
}
#pragma mark ----------选完图片后自动执行的方法------------
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{  //得到图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.background.image = image;
  //如果调用的是相机则保存图片
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        //保存图片
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
